/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 27.11.12
 * Time: 10:41
 * To change this template use File | Settings | File Templates.
 */
package zakladki.AS3.country {
import AS3.constants.ChooseCountryState;

import avmplus.implementsXml;

import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Dictionary;

import mx.controls.Alert;

import zakladki.decorators.PlotItemDecorator;

public class GDPPlotItemClickListener {

    private var worldPlotFlagMap:Dictionary = new Dictionary();
    private var europePlotFlagMap:Dictionary = new Dictionary();
    private var greenAsiaPlotFlagMap:Dictionary = new Dictionary();
    private var pinkAsiaPlotFlagMap:Dictionary = new Dictionary();
    private var yellowAsiaPlotFlagMap:Dictionary = new Dictionary();
    private var southAmericaPlotFlagMap:Dictionary = new Dictionary();
    private var northAmericaPlotFlagMap:Dictionary = new Dictionary();
    private var africaPlotFlagMap:Dictionary = new Dictionary();
    private var australiaPlotFlagMap:Dictionary = new Dictionary();

    private var allDictionaryMaps:Dictionary = new Dictionary();

    public function GDPPlotItemClickListener() {
        allDictionaryMaps[ChooseCountryState.AFRICA] = africaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.NORTH_AMERICA] = northAmericaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.SOUTH_AMERICA] = southAmericaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.AUSTRALIA] = australiaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.ASIA_GREEN] = greenAsiaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.ASIA_PINK] = pinkAsiaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.ASIA_YELLOW] = yellowAsiaPlotFlagMap;
        allDictionaryMaps[ChooseCountryState.EUROPE]  = europePlotFlagMap;
        allDictionaryMaps[ChooseCountryState.WORLD_MAP] = worldPlotFlagMap;
    }

    public function addPlotDataPoint(regionName:String,countryName:String,plotItem:PlotItemDecorator):void {
        if(allDictionaryMaps.hasOwnProperty(regionName)){
            var plotFlagMap:Dictionary = allDictionaryMaps[regionName];
            if(!plotFlagMap.hasOwnProperty(countryName)){
                plotFlagMap[countryName] = plotItem;
            }
        }
    }

    public function getNearestPlotItem(region:String,x:Number,y:Number):PlotItemDecorator {
        var clickedItem:PlotItemDecorator = null;
        if(allDictionaryMaps.hasOwnProperty(region)){
            var plotFlagMap:Dictionary = allDictionaryMaps[region];
            var closest:Number = 10000000000;
            var clickedCountry:String = null;
            for (var ctrName:Object in plotFlagMap) {
                var plotItem:PlotItemDecorator = plotFlagMap[ctrName];
                var rectangle:Rectangle = plotItem.rectangle;
                var center:Point = new Point(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2);
                var distance:Number = Math.pow(center.x - x, 2) + Math.pow(center.y - y, 2);
                if (distance < closest) {
                    closest = distance;
                    clickedItem = plotItem;
                    clickedCountry = ctrName.toString();
                }
            }
        }
        return clickedItem;
    }


}
}
